package com.example.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.R;
import com.example.reggie.entity.Category;
import com.example.reggie.entity.Setmeal;
import com.example.reggie.entity.SetmealDish;
import com.example.reggie.entity.SetmealDto;
import com.example.reggie.service.impl.CategoryServiceImpl;
import com.example.reggie.service.impl.SetmealDishServiceImpl;
import com.example.reggie.service.impl.SetmealServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealServiceImpl setmealService;

    @Autowired
    private CategoryServiceImpl categoryService;

    @Autowired
    private SetmealDishServiceImpl setmealDishService;

    /*分页查询*/
    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,String name){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null,Setmeal::getName,name);

        IPage<Setmeal> iPage = setmealService.page(new Page<>(page, pageSize), wrapper);

        /*获取数据*/
        List<Setmeal> records = iPage.getRecords();

        Map map = new HashMap();

        ArrayList<SetmealDto> list = new ArrayList<>();


        for (Setmeal record : records) {
            Long categoryId = record.getCategoryId();
            Category byId = categoryService.getById(categoryId);

            String byIdName = byId.getName();

            SetmealDto setmealDto = new SetmealDto();
            setmealDto.setCategoryName(byIdName);

            BeanUtils.copyProperties(record,setmealDto);
            list.add(setmealDto);
        }
        map.put("records",list);

        return R.success(map);

    }

    /*新增套餐接口*/
    @PostMapping
    @CacheEvict(value = "SetCacheable",key = "'set_'+#setmealDto.categoryId")
    public R add(@RequestBody SetmealDto setmealDto){

        setmealService.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        Long id = setmealDto.getId();

        for (SetmealDish setmealDish : setmealDishes) {
            /*获取的是Setmeal的Id是为了让SetmealDish和SetmealDish关联*/

            setmealDish.setSetmealId(id);
            setmealDishService.save(setmealDish);
        }

        return R.success("添加成功");
    }
    /*删除套餐*/
    @DeleteMapping
    @CacheEvict(value = "SetCacheable",allEntries = true)
    public R deleted(@RequestParam ArrayList<Long> ids){

        List<Setmeal> list = setmealService.listByIds(ids);

        for (Setmeal setmeal : list) {
            Integer status = setmeal.getStatus();
            if (status==0){
                setmealService.removeById(setmeal);
                setmealDishService.removeById(setmeal);
            }else {
                return R.error("该套餐处于启用状态,请禁用后删除");
            }
        }

        return R.success("删除成功");
    }

    /*批量禁用和启用*/
    @PostMapping("/status/{status}")
    @CacheEvict(value = "SetCacheable",allEntries = true)
    public R status(@PathVariable Integer status, @RequestParam ArrayList<Long> ids){

        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);

            setmealService.updateById(setmeal);
        }
        return R.success("修改成功");
    }

    /*回显数据*/
    @GetMapping("/{id}")
    public R huixian(@PathVariable Long id){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getId,id);
        Setmeal serviceOne = setmealService.getOne(wrapper);

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);


        SetmealDto setmealDto = new SetmealDto();
        setmealDto.setSetmealDishes(list);

        BeanUtils.copyProperties(serviceOne,setmealDto);


        return R.success(setmealDto);
    }

    /*修改*/
    @PutMapping
    @CacheEvict(value = "SetCacheable",key = "'set_'+#setmealDto.categoryId")
    public R up(@RequestBody SetmealDto setmealDto){
        setmealService.updateById(setmealDto);


        System.out.println(setmealDto.getId());
        log.info("sdawds"+setmealDto.getId());
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishService.remove(wrapper);


        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {

            setmealDish.setSetmealId(setmealDto.getId());

            setmealDishService.save(setmealDish);
        }

        return R.success("修改成功");
    }

    /*手机端查询*/
    /*condition：条件，满足条件时才缓存数据
      unless：满足条件则不缓存
      #result代表的是返回的值
      */
    @GetMapping("/list")
    @Cacheable(value = "SetCacheable",key ="'set_'+#categoryId",unless ="#result.data.size()==0")
    public R<List<Setmeal>> list(Long categoryId,Long status){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId!=null,Setmeal::getCategoryId,categoryId);
        wrapper.eq(status!=null,Setmeal::getStatus,1);
        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);
    }
}
